#!/bin/csh -f
# jfd@Tue May 12 15:13:11 WDT 1998
#
# Shell script to analyse the completeness of NOE distance
# restraints, using AquaCompl and other Aqua modules.
# The results will be shown as links in the overview file.
#
# The variable 'aquaroot' must exist and point to the Aqua top directory.
# The 'aqsetup' script should reside in that directory.
#

if ( $#argv != 3 ) then
    echo "Number of arguments found ($#argv) is not correct"
    goto usage
endif

goto begin

#####################
# usage block
#####################
usage:
    echo 'usage: compl_chk.csh  dir_numb  pdb_type  terseness'
    echo ''
    echo '   dir_numb:  number of directory at the server side'
    echo '   pdb_type:  -pdb, -xplor, or -molmol'
    echo '   terseness: 0 through 9'
    
    goto ending


#####################
# initialization
#####################
begin:
# SAME AS IN PERL SCRIPTS, MANY ARE REDUNDANT TO THIS PROGRAM

# SITE DEPENDANT
    set serv_loc        = "AQUA_LOC/Aqua/server/"
    set serv_dir        = "AQUA_DIR/Aqua/server/"

    set cgi_bin_loc     = "CGI_LOC/Aqua/"
    set cgi_bin_dir     = "CGI_DIR/Aqua/"

    set tmp_loc         = "TMP_LOC/Aqua/"
    set tmp_dir         = "TMP_DIR/Aqua/"
    set grace_home      = "GRACE_LOC"
    set webmaster       = "WEBMASTER"

# SITE INDEPENDENT
    set author          = "jurgen\@bmrb.wisc.edu"

    set overview        = "overview.html"
    set logfile         = "server.log"
    set debug           = "false"


    setenv aquaroot $serv_dir/..
    setenv GRACE_HOME $grace_home
    if ( $debug == "true" ) then
        echo 'Aqua program directory:' $aquaroot
    endif
    source $aquaroot/aqsetup

    umask 0000              # File creation mask
    limit cputime     60    # Maximum number of seconds the CPU can spend
                            # on any single process spawned.
                            # This should be half the $cal_time_secs
                            # defined in the calling cgi script.
    limit filesize   100m   # Maximum size of any one file
    limit datasize   100m   # Maximum size of data (including stack)
    limit coredumpsize 0    # Maximum size of core dump file

    echo 'DETAILS CAN BE FOUND IN THE LOG FILES: complchk.log AND aqpc.log'
    echo ' '
    echo 'COMPLCHK - Analyzing the completeness of NOE restraints'

    set path = ($path /usr/share/bin /usr/sbin \
                  $cgi_bin_dir )
# For Utrecht locations of ?
    set path = ( /usr/ruuci11/people/ruu/bin $path )
# For Heidelberg xmgr location
    set path = ( /usr/pub/xmgr/bin $path )
# For Madison xmgr location
    set path = ( /share/linux/bin $path )
    
# for the convert program in Utrecht (Still valid?)
    setenv ruu2 /nmr7/ruu2
    

#####################
# process the options
#####################
options:
switch ( "$2" )
    case "-pdb":
	set pdbtype = ""
	breaksw
    case "-xplor":
	set pdbtype = "-xplor"
	breaksw
    case "-molmol":
	set pdbtype = "-molmol"
	breaksw
    default:
        goto usage
endsw
    
set terseness = "$3"
    
# go to the right dir
    set dir = $tmp_dir/$1

    if ( !(-e $dir ) )then
	echo "Directory $dir doesn't exist"
        goto ending
    endif
    
    cd $dir
    
# set coordinates file
    set coords  = "struct"
    
# setup project name
    set proj = `qgetproj $coords`
    echo 'COMPLCHK - Project name is' $proj
    qproject $proj

# convert uploaded dos files to unix. Macs might go wrong here?
    gawk '{gsub (/\015/,"",$0);print}' $proj.pdb > $proj.pdb.tmp
    \mv $proj.pdb.tmp $proj.pdb
    gawk '{gsub (/\015/,"",$0);print}' $proj.noe > $proj.noe.tmp
    \mv $proj.noe.tmp $proj.noe
    
# prepare for completeness check, and run complchk

    echo 'COMPLCHK - Running complchk'

    if ( $debug == "true" ) then
        echo 'executing:'
        echo "\
              complchk -terseness $terseness $pdbtype $coords.pdb"
    endif
    complchk -terseness $terseness $pdbtype $coords.pdb

# prepare a figure per residue
    
    set result = "true"
    
    if ( !(-e struct_comp_str.comp) ) then
	set result = "false"
	echo 'COMPLCHK - ERROR no resulting comp file created'
	goto ending
    endif

    echo 'COMPLCHK - prepare some figures per residue'
   
    set plot_list  = (  completeness_per_residue \
                        observable_restraints_per_residue)
    set block_list = ( 'COMPLETENESS_PER_RESIDUE$' \
		       'COMPLETENESS_PER_RESIDUE$' )
    set item_list  = ( 7 6 )
    
    @ count = 0
    foreach plot ( $plot_list )
	@ count++
	set block = $block_list[$count]
	set item  = $item_list[$count]
    
        qdbext_non_int -v b=$block struct_comp_str.comp > temp
	
	echo '@xaxis label "Residues"'                            > $plot.dat
	echo "@yaxis label "'"'$plot'"'                           >> $plot.dat
	echo '@xaxis label char size 1.3'                         >> $plot.dat
	echo '@yaxis label char size 1.3'                         >> $plot.dat
	echo '@default linestyle 1'                               >> $plot.dat
	echo '@s0 symbol 2'                                       >> $plot.dat
	echo '@s0 symbol size 0.50'                               >> $plot.dat
	echo '@s0 symbol fill 1'                                  >> $plot.dat
	
	gawk -v item=$item '{print $3, $(item)}' temp             >> $plot.dat
	
        \rm temp
	
#grbatch -printfile $plot.eps -device 2 -eps $plot.dat
	gracebat -printfile $plot.eps $plot.dat

	if ( !(-e $plot.eps) ) then
	    set result = "false"
	    echo 'COMPLCHK - ERROR no eps file created'
	endif

#   Warning present in Heidelberg redirected
	convert -rotate 90 -density 70x70 $plot.eps $plot.jpg >& /dev/null

	#\rm $plot.dat $plot.eps

	if ( !(-e $plot.jpg) ) then
	    set result = "false"
	    echo 'COMPLCHK - ERROR no gif file created'
	endif
    end
    
ending:
#   Update the result file

    if ( ! $?result ) then
        set result = "false"
    endif

    if ( $result == "true") then    
	set block = "SUMMARY_OVERALL"
	set item  = "C"
	set completeness = (`qdbext_non_int -v b=$block -v i=$item struct_comp_str.comp`)
    else
	set completeness = "Not_a_Number"
    endif

    echo 'COMPLCHK - Update the result file in dir' $1
    echo 'COMPLCHK - result is                    ' $result
    echo 'COMPLCHK - completeness is              ' $completeness

    echo 'Finished completeness check'

    touch finished # This file will be checked for by the perl script
    
    exit(0)
